﻿using ConfExpert.Forms.Rapor;
using ConfExpert.Utility;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.IO;

namespace ConfExpert.Forms.SatinAlma.YeniYapi
{
    public partial class POOlustur : XPopupForm
    {
        DataSet dsFoy = null;
        int poSipId;
        string poNr;
        int VendorId;
        bool Teklif = false;
        DateTime time;
        Byte[] lastFile;

        public POOlustur(DataSet dsFoy)
        {
            InitializeComponent();

            this.Teklif = true;
            this.dsFoy = dsFoy;
            lblVeren.Text = "Teklifi İsteyen";
        }

        public POOlustur(DataSet dsFoy, int poSipId, string poNr, int VendorId)
        {
            InitializeComponent();

            this.poSipId = poSipId;
            this.poNr = poNr;
            this.VendorId = VendorId;
            this.dsFoy = dsFoy;
        }

        private void POOlustur_Load(object sender, EventArgs e)
        {
            gleVeren.Properties.Assign(RI(Rp.V_User));
            gleUretici.Properties.Assign(RI(Rp.V_VendorK));
            gleVeren.EditValue = WhoAmI;
            this.Shown += POOlustur_Shown;

            if (Teklif)
            {
                this.Text = "Fiyat Teklif Talep Föyü";
                DataRow drHeader = dsFoy.Tables[0].Rows[0];
                drHeader["Vendor"] = gleUretici.Text;
                drHeader["TalepTipi"] = "Fiyat Teklifi";
                btnSend.Caption = "Teklif Föyü Oluştur";
                gleUretici.ReadOnly = false;
                txtSubject.Text = "Fiyat Teklifi Talebi";
                txtMessage.HtmlText = @"<html><head><meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><style type=""text/css"">.st1{font-family:Calibri;font-size:11pt}</style></head><body><span class=""st1"">Ekteki sipariş için fiyat teklifi rica ediyorum.</span></body></html>";
            }
            else
            {
                this.Text = poNr + " Sipariş Föyü";
                this.Height = 260;
                gleUretici.EditValue = VendorId;
                gleUretici.ReadOnly = true;
                txtSubject.Text = "ConfExpert PO." + poNr;
                txtMessage.HtmlText = @"<html><head><meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><style type=""text/css"">.st1{font-family:Calibri;font-size:11pt}</style></head><body><span class=""st1"">Ekteki sipariş üreticiye gönderilmiştir.</span></body></html>";
            }
        }

        void POOlustur_Shown(object sender, EventArgs e)
        {
            txtMessage.Focus();
            txtMessage.Document.CaretPosition = txtMessage.Document.CreatePosition(txtMessage.Text.Length);
        }
        private void btnSend_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string FileName = string.Empty;
            if (Teklif)
            {
                FoyOlustur();
                FileName = "Teklif." + DateTime.Now.ToString("yyyyMMddHHmm") + ".pdf";
            }
            else
            {
                if (lastFile == null)
                    FoyOlustur();

                FileName = poNr + ".pdf";
                ExecuteNonQuery("UpdPOSip", poSipId, lastFile, time, gleVeren.EditValue.ToInt(), txtNot.Text, txtIlgili.Text);
            }

            string to = "";
            string cc = "";
            try
            {
                to = Session.CurrentUser.Email.ToStr();
                DataRow r = ExecuteDataTable("GetPOKime2", poNr, to).Rows[0];
                cc = r["Kime"].ToStr();
                string subject = txtSubject.Text;
                Mail.Send(to, subject, txtMessage.HtmlText, cc, lastFile, FileName, to);

                Mail.Send("meyzi.dilsen@karbel.com", subject + r["Cust"].ToStr(), txtMessage.HtmlText, "", lastFile, FileName, "meyzi.dilsen@karbel.com");  //Meyzi hanımın subject'i farklı olduğu için maili ayrıldı.
            }
            catch (Exception ex)
            {
                ShowMessage("Sipariş oluşturuldu ama bilgilendirme e-postaları gönderilemedi !!! Hata:to:" + to + ";cc=" + cc + ";Ex:" + ex.ToStr());
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnFoy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FoyOlustur(true);
        }


        public byte[] FoyOlustur(bool goster = false)
        {
            time = DateTime.Now;

            DataRow drHeader = dsFoy.Tables[0].Rows[0];
            drHeader["SipGecen"] = AppSession.Lookups.V_User.FindByUserId(gleVeren.EditValue.ToInt()).Name;
            drHeader["Ilgili"] = txtIlgili.Text;
            drHeader["SiparisTarihi"] = DateTime.Now.ToString("dd.MM.yyyy HH:mm");
            drHeader["FoyNotu"] = txtNot.Text;

            KumasSiparisFoyu ks = new KumasSiparisFoyu();
            ks.KumasSiparisFoyuOlustur(dsFoy);

            ks.CreateDocument();

            if (goster)
                ks.ShowPreviewDialog();

            MemoryStream ms = new System.IO.MemoryStream();
            ks.PrintingSystem.ExportToPdf(ms);
            lastFile = ms.ToArray();

            return lastFile;
        }

        private void gleUretici_EditValueChanged(object sender, EventArgs e)
        {
            dsFoy.Tables[0].Rows[0]["Vendor"] = gleUretici.Text;
        }
    }
}